
function isEmpty(v) {
    return v == undefined || v == null;
}


export default {
    validatorRequired(func, message, rule, value, callback) {
        let funcValue = func ? func() : null;
        if (!funcValue) {
            callback(message);
        } else {
            callback();
        }
    },
 

    validatorArray(func, message, rule, value, callback) {

        let funcValue = func ? func() : null;
        if (!funcValue || funcValue.length == 0) {
            callback(message);
        } else {
            callback();
        }
    },

    append(rules, rule) {
        rules.push(rule)
        return rules
    },
    appendRules(rules, ruleList) {
        for (const rule of ruleList) {
            rules.push(rule)
        }
        return rules
    },

    funcRequired(trigger, func, message) {
        return {
            trigger, validator: (rule, value, callback) => {
                this.validatorRequired(func, message, rule, value, callback)
            }
        }
    },
    funcRequireds(func, message) {
        let rules = []
        this.append(rules, this.funcRequired('blur', func, message))
        this.append(rules, this.funcRequired('change', func, message))
        return rules;
    },


    funcArray(trigger, func, message) {
        return {
            trigger, validator: (rule, value, callback) => {
                this.validatorArray(func, message, rule, value, callback)
            }
        }
    },
    funcArrays(func, message) {
        let rules = []
        // this.append(rules, this.arrays(message))
        this.append(rules, this.funcArray('blur', func, message))
        this.append(rules, this.funcArray('change', func, message))
        return rules;
    },


    equal(trigger, valueFunc, message) {
        return {
            trigger, validator: (rule, value, callback) => {
                let ev = valueFunc();
                if (isEmpty(ev)) {
                    callback();
                } else if (value === ev) {
                    callback();
                } else {
                    callback(new Error(message));
                }
            }
        }
    },
    equals(valueFunc, message) {
        let rules = []
        this.append(rules, this.equal('blur', valueFunc, message))
        this.append(rules, this.equal('change', valueFunc, message))
        return rules;
    },


    array(trigger, message) {
        return { type: 'array', trigger, message, required: true }
    },
    arrays(message) {
        let rules = []
        this.append(rules, this.array('blur', message))
        this.append(rules, this.array('change', message))
        return rules;
    },

    required(trigger, message) {
        return { required: true, message, trigger }
    },
    requireds(message) {
        let rules = []
        this.append(rules, this.required('blur', message))
        this.append(rules, this.required('change', message))
        return rules;
    },
    length(trigger, name, min, max, message) {
        let isEmptyMin = isEmpty(min);
        let isEmptyMax = isEmpty(max);
        if (!isEmptyMin && !isEmptyMax) {
            return { trigger, min, max, message: message ? message : `${name}长度应该在 ${min} 到 ${max} 个字符之间` }
        } else if (!isEmptyMax) {
            return { trigger, min, max, message: message ? message : `${name}长度应该不超过 ${max} 个字符` }
        } else if (!isEmptyMin) {
            return { trigger, min, max, message: message ? message : `${name}长度应该多于 ${min} 个字符` }
        } else {
            return {}
        }

    },


    lengths(name, min, max, message) {
        let rules = []
        this.append(rules, this.length('blur', name, min, max, message));
        this.append(rules, this.length('change', name, min, max, message));
        return rules;
    },


    integer(message) {
        return this.patterns(/^[0-9]+$/, message ? message : `请输入整数`)
    },


    numbers(name, min, max, valueFunc, message) {
        let rules = []
        this.append(rules, this.number('blur', name, min, max, valueFunc, message));
        this.append(rules, this.number('change', name, min, max, valueFunc, message));
        return rules;
    },

    number(trigger, name, min, max, valueFunc, message) {
        let isEmptyMin = isEmpty(min);
        let isEmptyMax = isEmpty(max);

        return {
            trigger, validator: (rule, value, callback) => {
                let funcValue = Number(valueFunc());
                let msg = "";
                if (message) {
                    msg = message
                } else {
                    if (!isEmptyMin && !isEmptyMax) {
                        if (funcValue < min) {
                            msg = `${name}不应该小于${min}`;
                        }
                        if ( funcValue > max) {
                            msg = `${name}不应该大于${max}`;
                        }
                    } else if (!isEmptyMax) {
                        if (funcValue > max) {
                            msg = `${name}应不超过 ${max} `
                        }
                    } else if (!isEmptyMin) {
                        if (funcValue > max) {
                            msg = `${name}应大于 ${min} `
                        }
                    }
                }
                if (msg) {
                    callback(new Error(msg));
                } else {
                    callback();
                }
            }
        }
    },

    quantity(name, min, max, required, valueFunc, message) {
        required = isEmpty(required) ? true : required

        let rules = []
        if (required) {
            this.appendRules(rules, this.requireds(message ? message : `${name}不能为空`))
        }
        this.appendRules(rules, this.integer())
        this.appendRules(rules, this.numbers(name, min, max, valueFunc, message));
        return rules;
    },

    price(name, min, max, required, valueFunc, message) {
        required = isEmpty(required) ? true : required

        let rules = []
        if (required) {
            this.appendRules(rules, this.requireds(message ? message : `${name}不能为空`))
        }
        this.appendRules(rules, this.patterns(/^[0-9]+(\.[0-9]{0,2})?$/, `请输入正确的${name}, 最多可输入两位小数`))
        this.appendRules(rules, this.numbers(name, min, max, valueFunc, message)); 
        return rules;
    },

    pattern(trigger, pattern, message) {
        return { trigger, pattern, message };
    },
    patterns(pattern, message) {
        let rules = []
        this.append(rules, this.pattern('blur', pattern, message));
        this.append(rules, this.pattern('change', pattern, message));
        return rules;
    },
    code(name, min, max, required) {
        let rules = []
        required = isEmpty(required) ? true : required
        this.appendRules(rules, this.lengths(`${name}`, min, max))
        if (required) {
            this.appendRules(rules, this.requireds(`${name}不能为空`))
        }
        this.appendRules(rules, this.patterns(/^[0-9a-zA-Z]{1,}$/, `请输入正确的${name}, 仅支持数字和字母的组合`))
        return rules;
    },
    name(name, min, max, required) {
        let rules = []
        required = isEmpty(required) ? true : required
        this.appendRules(rules, this.lengths(`${name}`, min, max))
        if (required) {
            this.appendRules(rules, this.requireds(`${name}不能为空`))
        }
        return rules;
    },

    mobile(name, required) {
        name = name ? name : "手机号码"
        let len = 11;
        let message = `请输入${len}位${name}`;
        let rules = []
        required = isEmpty(required) ? true : required
        this.appendRules(rules, this.patterns(/\d{11}$/, message))
        this.appendRules(rules, this.lengths(null, len, len, message))
        if (required) {
            this.appendRules(rules, this.requireds(`${name}不能为空`))
        }
        return rules;
    },

 

    bothNumberAndLetter(name) {
        let rules = [] 
        this.appendRules(rules, this.patterns(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{0,}$/, `${name}必须是数字与字母的组合`)) 
        return rules;
    },

    password(name, min, max) {
        min = min ? min : 8
        max = max ? max : 32
        let rules = [] 
        this.appendRules(rules, this.requireds(`${name}不能为空`))
        this.appendRules(rules, this.lengths(name, min, max)) 
        this.appendRules(rules, this.bothNumberAndLetter(name)) 
        return rules;
    },

    desc(getRef, name, min, max, required) {
        const fn = function(rule, value, callback) {
            const ref = getRef()
            const quill = ref.$refs.quillEditors.quill
            const len = quill.getLength() - 1
            // console.log('====', len)
            if (len == 0) {
                return callback(new Error(`请输入${name}`))
            }else if(max && len > max){
                return callback(new Error(`${name}长度应该在 ${min} 到 ${max} 个字符之间`))
            }
            callback()
        }
        return [
            { required, validator: fn, trigger: ['blur', 'change'] }
        ]
    },
}

